package com.pzhu.user.controller.manage;

import com.pzhu.base.common.utils.result.PageData;
import com.pzhu.base.common.utils.result.Result;
import com.pzhu.base.common.utils.result.Results;
import com.pzhu.user.domain.ro.PermissionCreateRO;
import com.pzhu.user.domain.ro.PermissionSearchRO;
import com.pzhu.user.domain.ro.PermissionUpdateRO;
import com.pzhu.user.domain.vo.PermissionSearchVO;
import com.pzhu.user.service.impl.PermissionService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/manage/permissions")
@Validated
public class PermissionController {

    private final PermissionService permissionService;

    /**
     * 列表
     *
     * @param permissionSearchRo 权限搜索请求
     * @return {@link Result }<{@link Collection }<{@link PermissionSearchVO }>>
     */
    @GetMapping
    public Result<Collection<PermissionSearchVO>> list(PermissionSearchRO permissionSearchRo) {
        PageData<PermissionSearchVO> result = permissionService.search(permissionSearchRo);
        return Results.page(result);
    }

    /**
     * 信息
     *
     * @param permissionId 权限id
     * @return {@link Result }<{@link PermissionSearchVO }>
     */
    @GetMapping("/{permissionId}")
    public Result<PermissionSearchVO> info(@PathVariable Long permissionId) {
        PermissionSearchVO result = permissionService.info(permissionId);
        return Results.success(result);
    }

    /**
     * 创建
     *
     * @param permissionCreateRo 许可创建
     * @return {@link Result }<{@link Long }>
     */
    @PostMapping
    public Result<Long> create(@Valid @RequestBody PermissionCreateRO permissionCreateRo) {
        Long result = permissionService.create(permissionCreateRo);
        return Results.success(result);
    }

    /**
     * 更新
     *
     * @param permissionId 权限id
     * @param permissionUpdateRo 权限更新ro
     * @return {@link Result }<{@link Boolean }>
     */
    @PutMapping("/{permissionId}")
    public Result<Boolean> update(
            @PathVariable Long permissionId, @Valid @RequestBody PermissionUpdateRO permissionUpdateRo) {
        boolean result = permissionService.update(permissionId, permissionUpdateRo);
        return Results.success(result);
    }

    /**
     * 删除
     *
     * @param permissionId 权限id
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping("/{permissionId}")
    public Result<Boolean> delete(@PathVariable Long permissionId) {
        boolean result = permissionService.removeById(permissionId);
        return Results.success(result);
    }

    /**
     * 批量删除
     *
     * @param permissionIds 权限ID
     * @return {@link Result }<{@link Boolean }>
     */
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@NotBlank(message = "权限id列表不能为空！") String permissionIds) {
        List<Long> permissionIdList =
                Arrays.stream(permissionIds.split(",")).map(Long::valueOf).toList();
        boolean result = permissionService.removeByIds(permissionIdList);
        return Results.success(result);
    }
}
